/*
 * Copyright (c) 2000-2017 wanglei. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */

package com.xhb;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

import com.xhb.ui.resources.Resources;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.engine.ProprietaryFeature;
import com.tulskiy.keymaster.common.Provider;
import com.xhb.ui.Tab;
import com.xhb.ui.TabButton;
import com.xhb.ui.TabFactory;
import com.xhb.ui.TabbedPane;

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

/**
 * @author wanglei.
 */
public class EasyBrowserApplication {

    public static  Engine engine ;

    private static void initEnvironment() throws Exception {
       System.setProperty("jxbrowser.license.key",
                "请添加QQ 921888199 获取，或者自己去官网申请试用key");

        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "陕西小伙伴网络科技开源浏览器");
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    }

    public static void main(String[] args) throws Exception {
     // Creates Browser instance with context configured to use specified proxy settings.
        initEnvironment();
        EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED)
                .enableProprietaryFeature(ProprietaryFeature.AAC)
                .enableProprietaryFeature(ProprietaryFeature.H_264)
                .enableProprietaryFeature(ProprietaryFeature.WIDEVINE)
                .build();
        engine = Engine.newInstance(options);/*
        // Create the Browser
        Browser  browser = engine.newBrowser();*/
        Provider provider = Provider.getCurrentProvider(true);
        provider.reset();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                initAndDisplayUI();
            }
        });
    }

    private static void initAndDisplayUI() {
        final TabbedPane tabbedPane = new TabbedPane();
        Tab tab = TabFactory.createTab("http://www.cptcsoft.com/",engine.newBrowser());
        insertTab(tabbedPane, tab);
        insertNewTabButton(tabbedPane);
        JFrame frame = new JFrame("小伙伴浏览器");
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @SuppressWarnings("CallToSystemExit")
            @Override
            public void windowClosing(WindowEvent e) {
                tabbedPane.disposeAllTabs();
                System.exit(0);
            }
        });
        frame.add(tabbedPane, BorderLayout.CENTER);
        frame.setSize(1024, 700);
        frame.setLocationRelativeTo(null);
        frame.setIconImage(Resources.getIcon("jxbrowser16x16.png").getImage());
        frame.setVisible(true);
    }

    private static void insertNewTabButton(final TabbedPane tabbedPane) {
        TabButton button = new TabButton(Resources.getIcon("new-tab.png"), "New tab");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                insertTab(tabbedPane, TabFactory.createTab(engine.newBrowser()));
            }
        });
        tabbedPane.addTabButton(button);
    }

    private static void insertTab(TabbedPane tabbedPane, Tab tab) {
        tabbedPane.addTab(tab);
        tabbedPane.selectTab(tab);
    }
}
